﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Windows.Data.Xml.Dom;

namespace Beyondsoft.Tuanju.DataManager.DealProviders
{
    //<url>
    //    <loc>http://t.dianping.com/deal/32877</loc>
    //    <data>
    //        <display>
    //            <website>大众点评团</website>
    //            <siteurl>http://t.dianping.com</siteurl>
    //            <city>上海</city>
    //            <identifier>32877</identifier>
    //            <title><![CDATA[三人行骨头王火锅!仅售80元,价值100元代金券!全单抵用,可累计使用!15店通用!时尚环境品骨色骨香,三人携众尝创意川菜!]]></title>
    //            <shortTitle><![CDATA[三人行骨头王火锅]]></shortTitle>
    //            <category><![CDATA[美食]]></category>
    //            <subcategory><![CDATA[火锅]]></subcategory>
    //            <image>http://t1.dpfile.com/tuan/20120612/59164_129839676220000000_0.jpg</image>
    //            <startTime>1339516800</startTime>
    //            <endTime>1342108799</endTime>
    //            <value>100.00</value>
    //            <price>80.00</price>
    //            <description><![CDATA[三人行骨头王火锅!仅售80元,价值100元代金券!全单抵用,可累计使用!15店通用!时尚环境品骨色骨香,三人携众尝创意川菜!]]></description>
    //            <bought>5219</bought>
    //            <detail><![CDATA[【团购详情】凭大众点评网团购券可享受以下内容：- 三人行骨头王火锅代金券（仅售80元，价值100元）]]></detail>
    //            <priority>0</priority>
    //        </display>
    //    </data>	
    //</url>

    public class Dianping : DealProvider
    {
        private const string url = "http://api.t.dianping.com/n/api.xml?cityId={0}&page=1&count=50";

        protected override string Name
        {
            get { return "dianping"; }
        }

        protected override string ApiAddress
        {
            get { return url; }
        }

        protected override async Task<IEnumerable<Deal>> FetchDeals(City city)
        {
            string destUrl = string.Format(url, city.CityId);

            Uri uri = new Uri(destUrl);

            XmlDocument xdoc = await XmlDocument.LoadFromUriAsync(uri);

            XmlNodeList nodes = xdoc.SelectNodes("/urlset/url/data/display");

            List<Deal> deals = new List<Deal>();

            foreach (var node in nodes)
            {
                Deal deal = new Deal();

                deal.Website = node.SelectSingleNode("website").InnerText;
                deal.Gid = node.SelectSingleNode("identifier").InnerText;
                deal.Title = node.SelectSingleNode("title").InnerText;

                string category = node.SelectSingleNode("category").InnerText;
                deal.Category = await this.ConvertFrom(category);

                deal.Description = node.SelectSingleNode("description").InnerText;
                deal.Details = node.SelectSingleNode("detail").InnerText;
                deal.Price = double.Parse(node.SelectSingleNode("price").InnerText);
                deal.Value = double.Parse(node.SelectSingleNode("value").InnerText);
                deal.Bought = int.Parse(node.SelectSingleNode("bought").InnerText);
                deal.Image = node.SelectSingleNode("image").InnerText;
                deal.StartTime = long.Parse(node.SelectSingleNode("startTime").InnerText).ToDateTime();
                deal.EndTime = long.Parse(node.SelectSingleNode("endTime").InnerText).ToDateTime();
                deal.ShortTitle = node.SelectSingleNode("shortTitle").InnerText;

                deal.Discount = deal.Value == 0 ? 0 : Math.Ceiling(deal.Price / deal.Value * 100);
                deal.UriLink = "http://t.dianping.com/deal/" + deal.Gid;
                deal.ExpireTime = (deal.EndTime - DateTime.Today).Days;

                deals.Add(deal);
            }

            return deals;
        }
    }

    public static class Extension
    {
        public static DateTime ToDateTime(this long value)
        {
            DateTime defaultTime = new DateTime(1970, 1, 1);
            return defaultTime.AddSeconds(value);
        }
    }
}
